<template>
	<view>
		<nav-bar-sub></nav-bar-sub>
		<view>
			<view class="box">
				<view class="top">
					<view class="course">
						<view class="weight">
							所属课程:
						</view>
						{{data.courseName}}
					</view>
				</view>
				<view class="content">
					<u-parse :html="data.noteContent"></u-parse>
				</view>

				<view class="bottom">
					<view class="time">
						{{data.lastModifiedTime}}
					</view>
				</view>
			</view>
			<view class="button">
				<view class="edit">
					<u-button type="primary" @click="edit()">编辑</u-button>
				</view>
				<view class="delete">
					<u-button type="error" @click="deleteNote()">删除</u-button>
				</view>
			</view>
		</view>

		<view class="">
			<editor id="editor" class="ql-container" @input="getText" showImgToolbar @statuschange="onStatusChange" :read-only=false
			 @ready="onEditorReady">
				{{data.text}}
			</editor>
		</view>

		<view>
			<u-popup v-model="popup" mode="bottom">
				<view>
					<view class="poptext">
						<u-field v-model="data.noteContent" label="笔记" placeholder="请填写笔记" type="textarea">
						</u-field>
					</view>
					<view class="editbutton">
						<button type="default" @click="editFinish()">完成</button>
					</view>
				</view>
			</u-popup>
		</view>
		<view>
			<u-popup v-model="popupDelete" mode="center">
				<view class="popupDelete">
					<view class="warnText">
						删除后不可恢复，确认删除吗？
					</view>
					<view class="confirmButton">
						<view class="deletebutton">
							<u-button type="error" @click="deleteFinish()">删除</u-button>
						</view>
						<view class="canclebutton">
							<u-button type="default" @click="popupDelete = false">取消</u-button>
						</view>
					</view>
				</view>
			</u-popup>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				data: {},
				popup: false,
				popupDelete: false
			}
		},
		methods: {
			edit() {
				this.popup = true
			},
			deleteNote() {
				this.popupDelete = true
			},
			deleteFinish() {
				this.$api.delNoteBatch(this.$store.state.access_token, this.data.id)
					.then(res => {
						console.log(res);
						this.$refs.uToast.show({
							title: '删除成功',
							type: 'success'
						})
						this.popupDelete = false
						let pages = getCurrentPages(); //获取所有页面栈实例列表
						let prevPage = pages[pages.length - 2]; //上一页页面实例
						prevPage.$vm.refresh()
						uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
							delta: 1
						})
					})
			},
			editFinish() {
				this.$api.updateNotes(this.$store.state.access_token, this.data.id, this.data.noteContent)
					.then(res => {
						this.$refs.uToast.show({
							title: '编辑成功',
							type: 'success'
						})
						let pages = getCurrentPages(); //获取所有页面栈实例列表
						let prevPage = pages[pages.length - 2]; //上一页页面实例
						prevPage.$vm.refresh()
						uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
							delta: 1
						})
					})
				this.popup = false
			},
			onEditorReady() {
				var that = this;
				uni.createSelectorQuery().select('#editor').context((res) => {
					this.editorCtx = res.context;
					this.editorCtx.setContents({ //赋值
						html: that.data.text
					});
				}).exec()
			},
			getText(e) {
				var that = this;
				console.log(e.detail.html); //带标签内容
				console.log(e.detail.text); //纯文本内容
			}
		},
		onLoad: function(data) {
			this.data = data
			console.log(data);
		}
	}
</script>

<style lang="scss">
	.box {
		box-sizing: border-box;
		padding: 10rpx 10rpx 10rpx 10rpx;
		box-sizing: border-box;
		margin: 10px;
		box-shadow: 0 0 5px 1px rgba($color: #000000, $alpha: 0.1);
		border-radius: 5px;

		.top {
			box-sizing: border-box;
			padding-bottom: 15rpx;

			.course {
				display: flex;

				.weight {
					font-weight: bold;
				}
			}
		}

		.content {
			text-indent: 2em;
		}

		.bottom {
			display: flex;
			flex-direction: row-reverse;
			box-sizing: border-box;
			padding-top: 15rpx;

			.time {
				color: #BED3ED;
			}
		}

	}

	.popupDelete {
		box-sizing: border-box;
		padding: 50rpx;

		.confirmButton {
			padding-top: 35rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: space-evenly;

			.deletebutton {
				width: 45%;
			}

			.canclebutton {
				width: 45%;
			}

		}
	}

	.button {
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		position: fixed;
		bottom: 0;
		width: 100%;

		.edit {
			width: 50%;
		}

		.delete {
			width: 50%;
		}
	}
</style>
